﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Windows.Forms;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using Microsoft.WindowsAPICodePack.Dialogs;


namespace JDMon.ViewModels
{
    public partial class MainViewModel
    {
        [ObservableProperty]
        [NotifyCanExecuteChangedFor(nameof(UploadFileCommand))]
        private string uploadFilePathPC = string.Empty;
        private string[] uploadedFiles;
        [ObservableProperty]
        [NotifyCanExecuteChangedFor(nameof(UploadFileCommand))]
        private string uploadFolderPathCNC = "G:\\";

        [ObservableProperty]
        private string downloadFolderPathPC = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
        [ObservableProperty]
        private string downloadFilePathCNC = string.Empty;

        [ObservableProperty]
        private bool checkAddUploadedFileToTask = false;
        [ObservableProperty]
        private bool checkSetUploadedFileMainProg = false;

        [RelayCommand]
        private void ChooseUploadFilePathPC()
        {
            var dialog = new OpenFileDialog()
            {
                Title = "请选择上传文件",
                Filter = "刀轨程序|*.nc;*.tsk;*.eng;",
                Multiselect = true,
            };
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                uploadedFiles = dialog.FileNames;
                UploadFilePathPC = dialog.FileName;
            }
        }

        [RelayCommand]
        private void ChooseDownloadFolderPath()
        {
            var dialog = new CommonOpenFileDialog
            {
                IsFolderPicker = true,
                Title = "请选择本地计算机保存路径"
            };
            if (dialog.ShowDialog() == CommonFileDialogResult.Ok)
            {
                DownloadFolderPathPC = dialog.FileName;
            }
        }

        private bool CanUpload => jdMachMon.IsConnect() && !string.IsNullOrWhiteSpace(UploadFilePathPC) && !string.IsNullOrWhiteSpace(UploadFolderPathCNC);
        [RelayCommand(CanExecute = nameof(CanUpload))]
        private void UploadFile()
        {
            if (jdMachMon.IsConnect())
            {
                ShowMessageEvent($"Info: set upload folder on CNC as {UploadFolderPathCNC}");
                jdMachMon.SetMachRcvFolder(UploadFolderPathCNC);
                
                jdMachMon.GetMachRcvFolder(ref uploadFolderPathCNC);
                OnPropertyChanged(nameof(UploadFolderPathCNC));
                ShowMessageEvent($"Warn: upload folder on CNC is {UploadFolderPathCNC}");

                ShowMessageEvent($"Info: send {UploadFilePathPC} on PC to {UploadFolderPathCNC} on CNC");
                //jdMachMon.SendFile(UploadFilePathPC, true, false);
                bool result = true;
                foreach(var file in uploadedFiles)
                    result = result && jdMachMon.SendNcFile(file, false, false, false);

                if(result)
                    MessageBox.Show("已发送", "发送成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
                else
                    MessageBox.Show("错误", "存在文件发送失败", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else
                MessageBox.Show("错误", "未连接到机床", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

        private bool CanDownload => jdMachMon.IsConnect() && !string.IsNullOrWhiteSpace(DownloadFilePathCNC) && !string.IsNullOrWhiteSpace(DownloadFolderPathPC);
        [RelayCommand(CanExecute = nameof(CanDownload))]
        private void DownloadFile()
        {
            if (jdMachMon.IsConnect())
            {

            }
            else
            {
                MessageBox.Show("错误", "未连接到机床", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
    }
}
